var ns, app;
app = new Vue({
    el: '#app',
    data: {
        list: [],
        form: {
            keywords: '',//关键词
            pageindex: 0,
            pagesize: 10,
            servicetype: 1, //0-未选择 1-任务工单 2-投诉建议
            status: -1, //10-待处理 0-处理中 1-已完工 3-待抢单
            RoomID: 0,
            isviewdetail: true,
            onlybaoshi: 0,
            // isFirstLoad: 1,
            startTime: '',//开始时间
            endTime: '',//结束时间
            activeDate: 0,            
            activeDateDesc: '时间',
            ProjectStatusIDs: [],//筛选状态数组
            IsHistory:1,//1-历史工单
        },
        // timeform:{
        //     startTime: '',
        //     endTime: '',
        //     activeDate: 0,            
        //     activeDateDesc: '今日',
        //     ProjectStatusIDs: [],
        // },
        total: {
            WaitCount: 0,
            ProcessCount: 0,
            CompleteCount: 0,
            TotalCount: 0,
            OnFileCount: 0
        },
        current_item_length: 0,
        is_searching: false,
        scroll_top: 0,
        can_scroll: false,
        showsearch:false,
        showsearchmonth: false,
        showsearchparam: false,
        IsHistory:1, //历史工单
        projectList:[
            {"ID":"4","Name":"待受理"},
            {"ID":"5","Name":"待派单"},
            {"ID":"10","Name":"待接单"},
            {"ID":"0","Name":"处理中"},
            {"ID":"1","Name":"已完工"},
            {"ID":"50","Name":"已关单"},
        ],
    },
    methods: {
        get_data: function() {
            var that = this;
            that.is_searching = true;
            that.can_scroll = false;
            var options = {};
            options.RoomID = that.form.RoomID;
            options.ServiceType = that.form.servicetype;
            options.ServiceStatus = that.form.status;
            options.IsHistory = that.form.IsHistory;
            options.keywords = that.form.keywords;//关键词
            options.startTime = that.form.startTime;//开始时间
            options.endTime = that.form.endTime;//结束时间
            options.ProjectStatusIDs = JSON.stringify(that.form.ProjectStatusIDs);//状态筛选，支持多选和单选
            options.pagesize = that.form.pagesize;
            options.pageindex = that.form.pageindex;
            options.action = "getnewservicelist";
            options.onlybaoshi = that.form.onlybaoshi;
            options.isAnalysis = that.form.isviewdetail ? 1 : 0;
            ns.post(options, function(succeed, data, err) {
                that.is_searching = false;
                if (succeed) {
                    if (data.data.ServiceList.length == that.form.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.form.pageindex == 0) {
                        that.list = data.data.ServiceList;
                    } else {
                        that.list = that.list.concat(data.data.ServiceList);
                    }
                    app.current_item_length = that.list.length;
                    
                   
                } else if (err) {
                    that.list = [];
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        openGongdan: function(item) {
            var that = this;
            ns.openWin('home_task_con_frm', '任务', {
                id: item.ID,
                isviewdetail: that.form.isviewdetail
            });
        },
        doSelectSearchParam: function(type) {
            var that = this;
            if (type == 1) { //筛选
                that.showsearchparam = !that.showsearchparam;
                
                that.showsearchmonth = false;
            }
            if (type == 2) { //按月
                that.showsearchparam = false;
                that.showsearchmonth = !that.showsearchmonth;
            }
            that.showsearch = that.showsearchparam || that.showsearchmonth;
            if (type == 2 && that.showsearchmonth) {
                setTimeout(function () {
                    that.initPicker();
                }, 100);
            }
        },
        doHideSearch: function() {
            var that = this;
            that.showsearchparam = false;
            that.showsearchmonth = false;
            that.showsearch = false;
        },
        doClickDay: function(dataValue) {
            var that = this;
            that.form.activeDate = dataValue;
            var startDate = '',
                endDate = '';
            if (dataValue == 1) {
                var currentDate = dateRangeUtil.getCurrentDate();
                startDate = dateRangeUtil.getDateString(currentDate);
                endDate = startDate;
                that.form.activeDateDesc = '今日';
            }
            if (dataValue == 2) {
                var currentWeek = dateRangeUtil.getCurrentWeek();
                startDate = dateRangeUtil.getDateString(currentWeek[0]);
                endDate = dateRangeUtil.getDateString(currentWeek[1]);
                that.form.activeDateDesc = '本周';
            }
            if (dataValue == 3) {
                var currentMonth = dateRangeUtil.getCurrentMonth();
                startDate = dateRangeUtil.getDateString(currentMonth[0]);
                endDate = dateRangeUtil.getDateString(currentMonth[1]);
                that.form.activeDateDesc = '本月';
            }
           
            that.form.startTime = startDate;
            that.form.endTime = endDate;
        },
        do_select_time: function(type) {
            var that = this;
            var currentTime = type == 1 ? that.form.startTime : that.form.endTime;
            api.openPicker({
                type: 'date',
                date: currentTime,
                title: '选择日期'
            }, function(ret, err) {
                if (ret) {
                    var year = ret.year;
                    var month = (ret.month >= 10 ? ret.month : '0' + ret.month);
                    var day = (ret.day >= 10 ? ret.day : '0' + ret.day);
                    currentTime = year + '-' + month + '-' + day;
                    if (type == 1) {
                        that.form.startTime = currentTime;
                    } else {
                        that.form.endTime = currentTime;
                    }
                }
            });
        },
        doSearch:function(){
            var that = this;
            that.doHideSearch();
            that.get_data();
        },
        doReset:function(type){
            var that = this;
            if (type == 2) {
                that.form.startTime = '';
                that.form.endTime = '';
                that.form.activeDate = 0;
                that.form.activeDateDesc='时间';
            } else {
                that.form.ProjectStatusIDs = [];
            }
        },
        
        doSelectProject: function(ID) {
            var that = this;
            var index = that.form.ProjectStatusIDs.indexOf(ID);
            if (index > -1) {
                that.form.ProjectStatusIDs.splice(index, 1);
            } else {
                that.form.ProjectStatusIDs.push(ID);
            }
        },
        do_search: function() {
            this.get_data();
        },
        // 受理
        do_open_shouli:function(id){
            var that = this;
            var options = {};
            options.ID = id;
            options.action = "customerserviceshouli";
            ns.post(options, function(succeed, data, err) {
                if (data.status==true) {
                    // that.form.isFirstLoad=1;
                    that.select_status(5);
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                } else {
                    api.toast({
                        msg: data.Msg,
                        duration: 2000,
                        location: 'bottom'
                    });
                    // that.form.isFirstLoad=1;
                    that.get_data();
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },
        //拒绝
        do_open_nodialog:function(id){
            var that=this;
            dialog.alert({
                title:"提示",
                msg:'拒绝接受该工单，工单将流转至派单人。是否确认拒绝？',
                buttons:['取消','确认拒绝']
            },function(ret){
                if(ret.buttonIndex==2){
                    var options = {};
                    options.ID=id;
                    options.action = "refuseservice";
                    ns.post(options, function(succeed, data, err) {
                        if (data.status==true) {
                            // that.form.isFirstLoad=1;
                            that.get_data();
                        }else {
                            api.toast({
                                msg: data.Msg,
                                duration: 2000,
                                location: 'bottom'
                            });
                            // that.form.isFirstLoad=1;
                            that.get_data();
                        }
                    }, {
                        toast: true,
                        toastmsg: '提交中'
                    });
                    
                }
            })

        },
        //接单
        do_accpet:function(item){
            var that = this;
            var options = {};
            options.ID = item.ID;
            options.action = "acceptservice";
            ns.post(options, function(succeed, data, err) {
                if (data.status==true) {
                    // that.form.isFirstLoad=1;
                    that.select_status(0);
                    // that.get_data();
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                } else {
                    api.toast({
                        msg:'状态已改变，请重新选择',
                        duration: 2000,
                        location: 'bottom'
                    });
                    // that.form.isFirstLoad=1;
                    that.get_data();
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },
        //催单
        do_open_cuidan:function(id){
            var that = this;
            var options = {};
            options.ID = id;
            options.action = "jpushuser";
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    toast.custom({
                        title:"催单成功",
                        html:'<i class="icon iconfont icon-yidu"></i>',
                        duration:2000
                    });

                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },
        // 派单
        do_open_chooseuser:function(item,type){
            var that = this;
            var options={};
            options.ID = item.ID; 
            options.action='getcustomerservicestatus';
            ns.post(options, function (succeed, data, err) {
                if (data.status==true&&data.ServiceStatus==item.ServiceStatus) {
                    ns.openWin('chooseserviceuser_frm', '重新派单', {
                        source: 'transfer',
                        id: item.ID,
                        type: type,
                        IsHistory:1,
                        ServiceType: that.form.servicetype
                    });
                    
                }else{
                    api.toast({
                        msg: '状态已改变，请重新选择',
                        duration: 2000,
                        location: 'bottom'
                    });
                    // that.form.isFirstLoad=1;
                    that.get_data();
                }
            });
        },
        //关单
        do_open_dialog:function(id){
            var that=this;
            dialog.alert({
                title:"提示",
                msg:'工单关闭后，将不能进行任何操作，请确认工单是否已经处理完成？',
                buttons:['取消','确认关闭']
            },function(ret){
                if(ret.buttonIndex==2){
                    var options = {};
                    options.ID=id;
                    options.action = "serviceclose";
                    ns.post(options, function(succeed, data, err) {
                        if (succeed) {
                            // that.form.isFirstLoad=1;
                            that.get_data();
                        }else if (err) {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }, {
                        toast: true,
                        toastmsg: '提交中'
                    });
                    
                }
            })
        },
        //完工
        do_complete:function(item){
            var that = this;
            var options = {};
            options.ID = item.ID;
            options.action = "getcustomerservicestatus";
            ns.post(options, function(succeed, data, err) {
                if (data.status==true&&data.ServiceStatus==item.ServiceStatus) {
                    ns.openWin('renwugongdancomplete_frm', '完工记录', {
                        source: 'transfer',
                        id: item.ID,
                        type: 2,
                        ServiceType: item.ServiceType
                    });
                } else {
                    api.toast({
                        msg:'状态已改变，请重新选择',
                        duration: 2000,
                        location: 'bottom'
                    });
                    // that.form.isFirstLoad=1;
                    that.get_data();
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });
           
        },
        //处理
        do_chuli:function(item){
            var that = this;
            ns.openWin('renwugongdancomplete_frm', '处理记录', {
                source: 'transfer',
                id: item.ID,
                type: 1,
                ServiceType: item.ServiceType
            });
        },
    //回访
        do_huifang:function(item){
            var that = this;
            ns.openWin('renwugongdanreturnvisit', '回访记录', {
                source: 'transfer',
                id: item.ID,
                type: 3,
                ServiceType: item.ServiceType
            });
        },
        initPicker: function () {
            var _this = this;
            (function ($, doc) {
                $.init();
                $.ready(function () {
                    //开始日期
                    var btn = document.querySelector('#startdate');
                    btn.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            _self.picker.show(function (rs) {
                                _this.form.startTime = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.startTime = rs.value;
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);
                    //结束日期
                    var btnEnd = document.querySelector('#enddate');
                    btnEnd.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            _self.picker.show(function (rs) {
                                _this.form.endTime = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.endTime = rs.value;
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);
                });
            })(mui, document);
        },

    }
});
apiready = function() {
    api.parseTapmode();
    dialog = new auiDialog();
    ns = window.Foresight.Util;
    app.form.servicetype = api.pageParam.servicetype || 1;
    app.form.RoomID = api.pageParam.id || 0;
    app.form.isviewdetail = api.pageParam.isviewdetail || false;
    if (app.form.isviewdetail) {
        app.form.status = 4;
    }
    app.form.onlybaoshi = api.pageParam.onlybaoshi || 0;
    api.addEventListener({
        name: 'reloadservicelist'
    }, function(ret, err) {
        app.get_data();
    });
    //app.doClickDay(1);
    ns.init_pull();
    ns.init_search();
    app.get_data();
    api.addEventListener({
        name: 'is_hidden_right',
     }, function(ret, err) {
        app.get_data();
    });
    api.addEventListener({
        name: 'do_open_neibubaoshi'
    }, function(ret, err) {
        ns.openWin('customerservice_history', '我的历史工单');
    });
};
